package com.kasinf.sample.sse.controller;

import com.kasinf.framework.common.pojo.CommonResult;
import com.kasinf.framework.sse.core.SseEmitterManager;
import com.kasinf.framework.sse.core.SseMessageDTO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author : Kaiser
 * @Date: 2025/6/25  10:25
 */
@RestController
@RequestMapping("sse")
public class SseSendController {

    @Resource
    private SseEmitterManager sseEmitterManager;

    /**
     * 向特定用户发送消息
     *
     * @param userId 目标用户的 ID
     * @param msg    要发送的消息内容
     */
    @PostMapping("send")
    public CommonResult<Boolean> send(Long userId, String msg) {
        SseMessageDTO dto = new SseMessageDTO();
        dto.setUserIds(List.of(userId));
        dto.setMessage(msg);
        sseEmitterManager.publishMessage(dto);
        return CommonResult.success(true);
    }

    /**
     * 向所有用户发送消息
     *
     * @param msg 要发送的消息内容
     */
    @PostMapping("sendAll")
    public CommonResult<Boolean> send(String msg) {
        sseEmitterManager.publishAll(msg);
        return CommonResult.success(true);
    }
}
